/**
 * delay call function
 * Copyright (c) 2009 cmiscm.com
 * date of final modification : 2009. 10. 20
*/
package com.cmiscm.utils.controls
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 * Delay Call.
	 * 
	 * @example
     * <listing>
     * package {
     *    import com.cmiscm.utils.controls.DelayCall;
     *    import flash.events.Event;
     * 
     *    public class TestDebug extends Sprite
     *    {
     *        private var _delayCall:DelayCall;
     *        
     *        public function TestDebug()
     *        {
     *            super();
     * 
     *            _delayCall = new DelayCall();
     *            _delayCall.call(2, delayAuto);
     *        }
     *        
     *        private function delayAuto(evt:Event):void
     *        {
     *            trace("delay call");
     *        }
     * 
     *        private function killDelay():void
     *        {
     *            if (_delayCall == null) {
     *                return;
     *            } else {
     *                _delayCall.cancel();
     *                _delayCall = null;
     *            }
     *        }
     *    }
     * }
     * </listing> 
     * 
     */
	public class DelayCall extends Object
	{
		private var _func:Function;
        private var _timer:Timer;
        
		public function DelayCall()
		{
			super();
		}
		
        /**
         * Delay Call Check Active
         * @return 
         * 
         */		
        public function get isActive():Boolean
        {
            if (this._timer != null) return true;
            return false;
        }

        /**
         * Cancel Delay Call 
         * 
         */		
        public function cancel():void
        {
            if (this._timer != null) {
                this._timer.stop();
                this._timer.removeEventListener("timer", this._func);
                this._timer.removeEventListener("timer", this.onComplete);
                this._timer = null;
            }
            if (this._func != null) this._func = null;
        }
		
        /**
         * Delay Call 
         * @param $param1 Delay time
         * @param $param2 Dealy function
         * 
         */		
        public function call($param1:Number, $param2:Function):void
        {
            this._timer = new Timer($param1 * 1000, 1);
            this._func = $param2;
            this._timer.addEventListener("timer", this._func);
            this._timer.addEventListener("timer", this.onComplete);
            this._timer.start();
        }

        private function onComplete(event:TimerEvent):void
        {
            this._timer.removeEventListener("timer", this._func);
            this._timer.removeEventListener("timer", this.onComplete);
            this._timer = null;
            this._func = null;
        }
		
	}
}